package com.example.yiwei.crm.view.activity.healthActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.ContextThemeWrapper;
import android.support.v7.widget.PopupMenu;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.data.GetActivity;
import com.example.yiwei.crm.entity.activityEntity;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.view.frament.activity.baseFragment;
import com.example.yiwei.crm.view.frament.activity.gridFragment;
import com.example.yiwei.crm.view.frament.activity.otherFragment;

import org.json.JSONException;
import org.json.JSONObject;

import static com.example.yiwei.crm.R.id.detailed_activity_btn_base;

public class detailedActivity extends AppCompatActivity {
    private ImageView img_back,img_more,img_phone,img_message,img_update;
    private TextView txt_name,txt_priority,txt_task_state,txt_car;
    private Button btn_base,btn_grid,btn_other;
    private PopupMenu popupMenu;
    FragmentManager manager;
    Fragment fragment_base,fragment_grid,fragment_other;
    boolean ButtonTag2 = false;
    boolean ButtonTag1 = false;
    boolean ButtonTag4 = false;
    activityEntity activityEntity;
    int activity_id;
    private Menu menu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detailed);
        img_back= (ImageView) findViewById(R.id.detailed_activity_img_back);
        img_more= (ImageView) findViewById(R.id.detailed_activity_img_more);
        img_update= (ImageView) findViewById(R.id.detailed_activity_img_update);
        txt_car= (TextView) findViewById(R.id.detailed_activity_txt_car);
        txt_name= (TextView) findViewById(R.id.detailed_activity_txt_name);
        txt_priority= (TextView) findViewById(R.id.detailed_activity_txt_priority);
        txt_task_state= (TextView) findViewById(R.id.detailed_activity_txt_task_state);
        btn_base= (Button) findViewById(detailed_activity_btn_base);
        btn_grid= (Button) findViewById(R.id.detailed_activity_btn_grid);
        btn_other= (Button) findViewById(R.id.detailed_activity_btn_other);
        manager = getSupportFragmentManager();
        fragment_base=new baseFragment();
        fragment_grid=new gridFragment();
        fragment_other=new otherFragment();
        OnClick(btn_base);
        popupMenu = new PopupMenu(this, findViewById(R.id.detailed_activity_img_more));
        menu = popupMenu.getMenu();
        final MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_activity, menu);
        activityEntity=GetActivity.get();
        txt_name.setText(activityEntity.getActivity_theme());
        txt_task_state.setText(activityEntity.getTask_state());
        txt_priority.setText(activityEntity.getPriority());
        activity_id=activityEntity.getActivity_id();

        //下拉框监听方法
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    //删除
                    case R.id.activity_delete:
                        AlertDialog.Builder builder;
                        builder = new AlertDialog.Builder(new ContextThemeWrapper(detailedActivity.this, R.style.custom_dialog));
                        builder.setTitle("您确定要删除吗？");
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        String result=NetWorkUtils.getCount(baseClass.delete_activity_message_url+activity_id,null);
                                        try {
                                            JSONObject object=new JSONObject(result);
                                            String boolen=object.getString("boolean");
                                            Log.d("lop",boolen);
                                            if ("success".equals(boolen)){
                                                finish();
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }).start();
                            }
                        });
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        });
                        builder.show();
                        break;
                }
                return false;
            }
        });
        img_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(detailedActivity.this,addActivity.class);
                intent.putExtra("tag","update");
                intent.putExtra("activity_id",activity_id);
                startActivity(intent);
            }
        });

    }
    public void OnClick(View view) {
        switch (view.getId()){
            case R.id.detailed_activity_btn_other:
                ButtonTag2=false;
                ButtonTag1 = true;
                ButtonTag4 = false;
                btn_other.setBackgroundColor(Color.parseColor("#FFFFFF"));
                btn_base.setBackgroundColor(Color.parseColor("#E1E8ED"));
                btn_grid.setBackgroundColor(Color.parseColor("#E1E8ED"));
                manager.beginTransaction().replace(R.id.detailed_activity_frame_conner,fragment_other).commit();
                break;
            case R.id.detailed_activity_btn_base:
                ButtonTag1 = false;
                ButtonTag2=true;
                ButtonTag4 = false;
                btn_other.setBackgroundColor(Color.parseColor("#E1E8ED"));
                btn_base.setBackgroundColor(Color.parseColor("#FFFFFF"));
                btn_grid.setBackgroundColor(Color.parseColor("#E1E8ED"));
                manager.beginTransaction().replace(R.id.detailed_activity_frame_conner,fragment_base).commit();
                break;
            case R.id.detailed_activity_btn_grid:
                ButtonTag1 = false;
                ButtonTag2=false;
                ButtonTag4 = true;
                btn_other.setBackgroundColor(Color.parseColor("#E1E8ED"));
                btn_base.setBackgroundColor(Color.parseColor("#E1E8ED"));
                btn_grid.setBackgroundColor(Color.parseColor("#FFFFFF"));
                manager.beginTransaction().replace(R.id.detailed_activity_frame_conner,fragment_grid).commit();
                break;
            case R.id.detailed_activity_img_more:
                popupMenu.show();
                break;

        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        activityEntity=GetActivity.get();
        txt_name.setText(activityEntity.getActivity_theme());
        txt_priority.setText(activityEntity.getPriority());
        txt_task_state.setText(activityEntity.getTask_state());
    }
}
